
var createError = require("http-errors");
var express = require("express");
var path = require("path"); //路径
var cookieParser = require("cookie-parser"); //cookie
var logger = require("morgan"); //日志
var app = express(); //启动express框架

// view engine setup
app.set("views", path.join(__dirname, "views")); //设置页面地址
app.set("view engine", "ejs"); //设置页面引擎.ejs 渲染规则

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public"))); //静态资源部署
app.use('/uploads', express.static(path.join(__dirname, "uploads")));

app.use("/system", require("./routes/system"));
app.use("/blog", require("./routes/blog"));
app.use("/project", require("./routes/project"));

// catch 404 and forward to error handler
// app.use('/public',express.static('public'));//将文件设置成静态

app.use(function (req, res, next) {
    console.log("异常URL：：：：：：：：：：", req.url);

    next(createError(404));
});

// 中间件：错误处理
app.use((err, req, res, next) => {
    console.log("发生异常：", err);
    res.status(500).json({ error: "服务器内部错误" });q
});

// error handler
app.use(function (err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get("env") === "development" ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render("error");
});


module.exports = app;
